import 'dart:io';
import 'package:process_run/shell.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:window_example/util/print_util.dart';

class LaunchUtils {
  static const _sTag = "LaunchUtils";

  ///_launchURL
  static void launchURL(String? slink) async {
    Uri link = Uri.parse(slink!);
    await canLaunchUrl(link)
        ? await launchUrl(link)
        : throw 'Could not launch $link';
  }

  static void launchAppByUri(String uri) async {
    var shell = Shell();
    if (!uri.contains("://")) {
      uri = "$uri://";
    }
    try {
      await shell.run('start "$uri"');
    } catch (e) {
      LoggerUtil.d(_sTag, "$uri launch error");
    }
  }

  static Future<void> launchAppByPath(String path) async {
    try {
      await Process.start(
        path,
        [],
        mode: ProcessStartMode.detached,
      );
    } catch (e) {
      LoggerUtil.d(_sTag, "launch error: $e");
    }
  }

  static void closeApp() {
    try {
      exit(0);
    } catch (e) {
      LoggerUtil.d(_sTag, "closeApp error: $e");
    }
  }
}
